#include <stdio.h>
#include<malloc.h>
#include<stdlib.h>
char ch='y';
int i;
int k=0;
int m;
int choice=1;
int o=0;
int size= 0;
//     a      works  as    array  due to relation bw array and pointer
int *a;
//








void append()
{int num;
printf("\n Enter the number");
scanf("%d",&num);
a= realloc(a,sizeof(int)* (size+1));
a[size]=num;
size++;
}


void display()
{
printf("[");
for(i=0;i<size;i++)
{

{if(i!=0)
printf(",");
}

printf("%d",a[i]);
}
printf("]");
}




void search()
{int flag=1;
int num;
printf("\n Enter the number to search");
scanf("%d",&num);
for(i=0;i<size;i++)
{
if(a[i] == num )
{flag=0;
break;
}
}
if (flag==1)
{printf("Error as value not found");
}
else
printf("The index position is %d",i);



}

void count()
{
int num;
int val =0;
printf("\n Enter the number to count");
scanf("%d",&num);
for(i=0;i<size;i++)
{
if(a[i] == num )
{
val =val +1;
}}
printf("The frequency is  is %d",val);



}



void insert()
{
int num=0;
int pos=0;
int index=0 ;
printf("\n Enter the number");
scanf("%d",&num);
a= realloc(a,sizeof(int)* (size+1));
printf("\n Enter index for appending");
scanf("%d",&pos);
index =pos;
size ++;
if(index <size)
{
for(i= size; i>index;i--)
{a[i] =a[i-1];
}
a[index]=num;
}
else
printf("Enter a valid index");

}

void remov()
{int flag;
int num,pos;
int index ;
printf("\n Enter the number");
scanf("%d",&num);
for(i=0;i<size;i++)
{
if(a[i] == num )
{flag=1;
break;
}
}
if(flag!=1)
printf("No element found");
else
{
index =i;

for(i= index+1; i<size;i++)
{a[i-1] =a[i];
}
size --;
}

}



void pop()
{
int num,pos;
int index ;
printf("\n Enter the index at which delete and if you enter -1 last element is deleted");
scanf("%d",&index);


if(index !=-1)
{printf("\n Value to be deleted is %d",a[index]);
for(i= index+1; i<size;i++)
{a[i-1] =a[i];
}
size --;
}
else
{
index =size;
printf("\n Value to be deleted is %d",a[size-1]);
for(i= index+1; i<size;i++)
{a[i-1] =a[i];
}
size --;
}
}




void sort()
{
int temp=0;
int j=0;
for(i=0;i<size-1;i++)
{ temp=0;

for(j=i+1;j<size;j++)
{

if (a[i]>a[j])
{
temp=a[i];
a[i] =a[j];
a[j]=temp;
}
}

}
}


void reverse()
{
int temp;
for(i=0;i<size/2;i++)

{temp=a[i];
a[i] =a[size-i-1];
a[size-i-1]=temp;
}
}

void extend(int m[],int noofelement)
{
int pos;
int index ;
int j=0;


a= realloc(a,sizeof(int)* (size+noofelement));

for(i= size,j=0; i<size + noofelement,j<=noofelement-1;i++,j++)
{
a[i] =m[j];
}
size=size+noofelement;
}



/*void initial()
{int num=0;
char ch ='y';
int size =0;


while(ch =='y' || ch == 'Y')
{
printf("\n Enter the number");
scanf("%d",&num);
p= realloc(p,sizeof(int)* (size+1));
p[size]=num;
size++;
printf("\n Press 'y' | 'Y' to enter more elements in list ");
scanf("%s",&ch);
}
for(i=0;i<size;i++)
{
printf("%d",p[i]);
}
}
*/

void main()
{
char ch= 'y';
int num,pos,val;
int sizen=0;
int i=0;
int p[29];
while (choice !=11)
{
printf("\n Enter your choice");
printf("\n Enter 1 to append");
printf("\n Enter 2 to extend");
printf("\n Enter 3 to insert");  
printf("\n Enter 4 to remove");
printf("\n Enter 5 to pop");
printf("\n Enter 6 to find search or get index");
printf("\n Enter 7 to count");
printf("\n Enter 8 to sort");
printf("\n Enter 9 to reverse");

printf("\n Enter 10 to display");
printf("\n Enter 11 to exit");


scanf("%d",&choice);


switch(choice)
{
case 1 :
    append();
        break;
case 2 :
printf("\n Enter the elements of array to merge ");
i=0;
sizen=0;
while(ch=='y'|| ch=='Y')
{
printf("\n Enter the number");
scanf("%d",&p[i]);
sizen++;
i++;
printf("\n Press 'y' | 'Y' to enter more elements in array to merge ");
scanf("%s",&ch);
}

extend(p,sizen);
ch='y';
break;

case 3: insert();
break;

case 4 : remov();
break;
case 6 :search();
break;
case 5: pop();
break;
case 9 :reverse();
break;

case 7 : count();
break;
case 8 :sort();
break;
case 10 :display();
       break;
case 11: exit(0);
break;
default: printf("\n Enter a valid choice");
		break;


}

}
}



